package omm.domain.comparativegraph;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import omm.domain.CommissionStrategy;
import omm.domain.Graphic;
import omm.domain.Quotation;

public class ComparativeGraphic implements Graphic {

	final private GraphicStrategy graphicStrategy;
	final private CommissionStrategy commissionStrategy;
	final private GroupStrategy groupStrategy;
	final private List<Quotation> quotations;

	public ComparativeGraphic(List<Quotation> quotations, GraphicStrategy graphicStrategy,
			CommissionStrategy commissionStrategy, GroupStrategy groupStrategy) {
		this.graphicStrategy = graphicStrategy;
		this.commissionStrategy = commissionStrategy;
		this.groupStrategy = groupStrategy;
		this.quotations = quotations;
	}

	private Map<String, Double> calculateData() {
		Map<String, Double> dataMap = new HashMap<String, Double>();

		Iterator<Quotation> it = quotations.iterator();
		while (it.hasNext()) {
			Quotation q = it.next();
			Map.Entry<String, Double> entry = groupStrategy.makeEntry(q);
			if (entry != null) {
				Double value = dataMap.get(entry.getKey());
				if (value == null)
					value = 0.0;
				dataMap.put(entry.getKey(), value + entry.getValue());
			}
		}

		return dataMap;
	}

	@Override
	public String getGraphicURL() {
		Map<String, Double> data = calculateData();
		return graphicStrategy.getGraphicURL(data, commissionStrategy);
	}

}
